<?php
// +----------------------------------------------------------------------
// | 分类页控制器
// +----------------------------------------------------------------------

class category_controller extends base_controller {

	protected $main_page_listrows = 16; //大分类页 每页显示的信息数
	protected $sub_page_listrows = 20; //大分类页 每页显示的信息数
	protected $tmp = array();

	public function _init(){
		
	}

	//分类首页
	public function index(){
		$catid = (int)$_GET['catid'];
		$page = (int)$_GET[C('var_page')];
		if($page<=0) $page = 1;
		if($catid<=0) redirect404();
		
		$cate = $this->category->getNode($catid);
		if($cate['pid']==0){
			$content = $this->_main($catid, $page);
		}else{
			$content = $this->_sub($catid, $page);
		}
		echo $content;
	}

	//获取大分类页面HTML
	public function _main($id,$page){
		//左边和底部公共数据
		$this->image->init();
		$this->getLeftData();
		$this->getFooterData();

		$var = array();
		$var['cate'] = $cate = $this->category->getNode($id);
		if(empty($cate)) return '';

		//当前位置
		$var['position'] = $this->category->getPositionHtml($id);

		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';

		//当前分类热门20条壁纸		
		$map = array();
		$map['catid1'] = $id;
		$rows = $this->image->getList($map, 20, $field, 'rand()', '', true);
		$var['list_hots'] = $this->image->format($rows,true);

		//小分类列表
		C('sys_page_listrows',$this->main_page_listrows);
		$conf = array('index_url' => url('category',$cate['id']), 'url' => url('category',$cate['id']).'{p}.html', 'setTheme' => 'en', 'theme'=>' %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%');
		$rows = $this->category->where("pid=$id")->order("sort asc,id asc")->page($page,$conf);
		$rows['lists'] = $this->category->format($rows['lists']);
		if($rows) $var = array_merge($var,$rows);

		//热门关键词
		$rows = $this->keywords->getHotKeywords(20,$cate['name']);
		$var['hot_keywords'] = $this->keywords->format($rows);

		$seo_page = $page>1 ? " page $page" : '';
		$this->assign('seo_title',$cate['name'].' Android & iPhone & iPad Wallpapers - Free '.$cate['name'].' Mobile Wallpapers & Pictures Download'.$seo_page);
		$this->assign('seo_keywords',$cate['name'].' Android & iPhone & iPad Wallpapers,'.$cate['name'].' wallpapers free download');
		$this->assign('seo_description','Free '.$cate['name'].' Android & iPhone & iPad wallpapers download, download free High Resolution, HD, Widescreen '.$cate['name'].' wallpapers and pictures.');

		$this->assign($var);
		$content = $this->fetch('category_main');
		return $content;
	}

	//获取小分类页面HTML
	public function _sub($id,$page){
		//左边和底部公共数据
		$this->image->init();
		$this->getLeftData();
		$this->getFooterData();
		
		$var = array();
		$var['cate'] = $cate = $this->category->getNode($id);
		if(empty($cate)) return '';

		//当前位置
		$var['position'] = $this->category->getPositionHtml($id);

		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';

		//当前分类热门20条壁纸
		$map = array();
		$map['catid2'] = $id;
		$map['status'] = 1;
		$rows = $this->image->getList($map, 20, $field, 'rand()', '', true);
		$var['list_hots'] = $this->image->format($rows,true);

		//图片列表
		C('sys_page_listrows',$this->sub_page_listrows);
		$conf = array('index_url' => url('category',$cate['id']), 'url' => url('category',$cate['id']).'{p}.html', 'setTheme' => 'en', 'theme'=>' %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%');
		$rows = $this->image->where($map)->order("id desc")->page($page,$conf);		
		$rows['lists'] = $this->image->format($rows['lists']);
		if($rows) $var = array_merge($var,$rows);

		//热门关键词
		$rows = $this->keywords->getHotKeywords(20,$cate['name']);
		$var['hot_keywords'] = $this->keywords->format($rows);

		$seo_page = $page>1 ? " page $page" : '';
		$this->assign('seo_title',$cate['name'].' Android & iPhone & iPad Wallpapers - Free '.$cate['name'].' Mobile Wallpapers & Pictures Download'.$seo_page);
		$this->assign('seo_keywords',$cate['name'].' Android & iPhone & iPad Wallpapers,'.$cate['name'].' wallpapers free download');
		$this->assign('seo_description','Free '.$cate['name'].' Android & iPhone & iPad wallpapers download, download free High Resolution, HD, Widescreen '.$cate['name'].' wallpapers and pictures.');

		$this->assign($var);
		$content = $this->fetch('category_sub');
		return $content;
	}

	//批量生成大分类页
	public function main(){		
		$this->chkCmd();
		$this->category->cacheDelete();

		$time1 = time();
		$cates = $this->category->getChildTree(0);
		$rows = array();
		if(is_numeric($_GET['catid']) && $_GET['catid']>0){
			$id = $_GET['catid'];
			if(!isset($cates[$id]))  exit('catid does not exist');
			$rows[$id] = $cates[$id];
			$cates = $rows;
		}

		//循环生成大分类页
		C('sys_page_listrows',$this->main_page_listrows);
		$page = new page();
		foreach($cates as $v){			
			echo "\n==================  ".date('Y-m-d H:i:s')." Main Category [{$v['name']}] create html!\n";
			$count = $this->category->where("pid='{$v['id']}'")->count();
			$page->init($count);
			for($p=1;$p<=$page->totalPages;$p++){
				$dir = $this->category->id2url($v['id']);
				$path = dirname(APP_PATH).$dir;
				$nowpage = $p==1 ? 'index.html' : "$p.html";
				$fileurl = $dir.$nowpage;
				$filepath = $path.$nowpage;

				//获取页面HTML
				$content = $this->_main($v['id'], $p);
				if(!is_dir($path)) mk_dir($path,0777);
				file_put_contents($filepath, $content);
				echo $fileurl."\n";
			}
		}
		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";

	}

	//批量生成小分类页
	public function sub(){
		$this->chkCmd();
		$this->category->cacheDelete();

		$time1 = time();
		$cates = $this->category->getChildTree(0);
		$cates_all = $this->category->getNodes();
		$rows = array();
		if(is_numeric($_GET['catid']) && $_GET['catid']>0){
			$id = $_GET['catid'];
			if(!isset($cates_all[$id]))  exit('catid does not exist');
			$cate = $this->category->getNode($id);
			if($cate['pid']==0){
				$rows[$id] = $cates[$id];
				$cates = $rows;
			}else{
				$rows[$cate['pid']] = $cates[$cate['pid']];
				$rows2[$id] = $cates[$cate['pid']]['child'][$id];
				unset($rows[$cate['pid']]['child']);
				$rows[$cate['pid']]['child'] = $rows2;
				$cates = $rows;
			}
		}

		//循环生成小分类页
		C('sys_page_listrows',$this->sub_page_listrows);
		$page = new page();		
		foreach($cates as $v){			
			if(empty($v['child'])) continue;

			//处理小分类生成
			foreach($v['child'] as $v2){
				echo "\n==================  ".date('Y-m-d H:i:s')." Sub Category  [{$v2['name']}] create html!\n";
				$count = $this->image->where("catid2='{$v2['id']}' and status=1")->count();
				$page->init($count);
				
				for($p=1;$p<=$page->totalPages;$p++){
					$dir = $this->category->id2url($v2['id']);
					$path = dirname(APP_PATH).$dir;
					$nowpage = $p==1 ? 'index.html' : "$p.html";
					$fileurl = $dir.$nowpage;
					$filepath = $path.$nowpage;

					//获取页面HTML
					$content = $this->_sub($v2['id'], $p);
					if(!is_dir($path)) mk_dir($path,0777);
					file_put_contents($filepath, $content);
					echo $fileurl."\n";
				}
			}		
			
		}
		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";

	}

	//生成abcd字母页
	public function letter(){
		$this->chkCmd();

		$time1 = time();
		$arr = $this->category->letter;
		if($_GET['letter'] && in_array($_GET['letter'], $this->category->letter)){
			$rows = array();
			$rows[$_GET['letter']] = $_GET['letter'];
			$arr = $rows;
		}

		$page = new page();
		foreach($arr as $v){
			echo "\n==================  ".date('Y-m-d H:i:s')." Letter  [ $v ] create html!\n";
			$map = $v=='other' ? "`name` regexp '^[^a-zA-Z]+' and status=1" : "`name` like '{$v}%' and status=1";
			$count = $this->image->where($map)->count();
			$page->init($count);

			for($p=1;$p<=$page->totalPages;$p++){
				$dir = '/glossary/'.strtolower($v).'/';
				$path = dirname(APP_PATH).$dir;
				$nowpage = $p==1 ? 'index.html' : "$p.html";
				$fileurl = $dir.$nowpage;
				$filepath = $path.$nowpage;

				//获取页面HTML
				$content = $this->_letter($v, $p);
				if(!is_dir($path)) mk_dir($path,0777);
				file_put_contents($filepath, $content);
				echo $fileurl."\n";
			}
		}

		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";
	}

	public function _letter($id,$page){
		//左边和底部公共数据
		$this->image->init();
		$this->getLeftData();
		$this->getFooterData();
		
		if(!in_array($id, $this->category->letter)) return '';

		$var = array();
		$letters = $this->category->getLetterList();
		$var['letter_cur'] = $letters[$id];

		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';

		//图片列表
		C('sys_page_listrows', 20);
		$conf = array('index_url' => url('letter',$id), 'url' => url('letter',$id).'{p}.html', 'setTheme' => 'en', 'theme'=>' %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%');
		$map = $id=='other' ? "`name` regexp '^[^a-zA-Z]+' and status=1" : "`name` like '{$id}%' and status=1";
		$rows = $this->image->where($map)->order("id desc")->page($page,$conf);
		$rows['lists'] = $this->image->format($rows['lists']);
		if($rows) $var = array_merge($var,$rows);

		//热门关键词
		if($this->tmp[$id]){
			$var['hot_keywords'] = $this->tmp['hot_keywords'];			
		}else{
			$rows = $this->keywords->getHotKeywords(20);
			$this->tmp[$id] = $id;
			$this->tmp['hot_keywords'] = $var['hot_keywords'] = $this->keywords->format($rows);
		}

		$seo_page = $page>1 ? " page $page" : '';
		$this->assign('seo_title','Free Android & iPhone & iPad Wallpapers Start With Letter '. ucfirst($var['letter_cur']['name']). $seo_page);
		$this->assign('seo_keywords',ucfirst($var['letter_cur']['name']).' Android & iPhone & iPad Wallpapers,'.ucfirst($var['letter_cur']['name']).' wallpapers free download');
		$this->assign('seo_description','Free '.ucfirst($var['letter_cur']['name']).' Android & iPhone & iPad wallpapers download, download free High Resolution, HD, Widescreen '.ucfirst($var['letter_cur']['name']).' wallpapers and pictures.');

		$this->assign($var);
		$content = $this->fetch('category_letter');
		return $content;
	}

}